{include file="header/index" /}
<style>
	.radiolist{
		display: flex;
		align-items: center;
	}
	.radiolist input{

	}
	.radiolist .layui-input-block{
		display: flex;
		align-items: center;
	}
	.row{
		margin: 0;
	}
	.applicant{
		line-height: 4px;
	}
	.customer_introducer_name_span{
		line-height: 4px;
	}
</style>
<!-- 加载动画 -->
<div class="spiner-example hides" style="position: fixed; top: 30px; left:50%; margin-left: -35px">
	<div class="sk-spinner sk-spinner-three-bounce">
		<div class="sk-bounce1"></div>
		<div class="sk-bounce2"></div>
		<div class="sk-bounce3"></div>
	</div>
</div>

<div class="container bgwhite">
	<h2 class="container-title mb40">编辑合同</h2>

	<form id="form" method="post" action="" enctype="multipart/form-data">
		<div class="clearfix">
			<p class="title mb20">主要信息</p>

			<div class="AddContract-center">
				<div class="row">
					<div class="form-group col-md-6 clearfix">
						<label>用户</label>
						<input onclick="openModel(1)" id="customer_name" class="no-input form-control" type="text" value="{$data.username}" readonly="readonly" />
						<span class="fs12 col-666 fl mt10 ml10">点击选择</span>
					</div>
					<div class="form-group col-md-6 clearfix">
						<label>申请人</label>
						<input type="text" value="{$data.applicant}" {if condition="isset($is_admin) AND $is_admin != 1"} readonly {/if} class="form-control" name="applicant" id="applicant" minlength="6" maxlength="11" placeholder="请输入申请人工号" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/>
						<span class="applicant fl mt15 ml10">{$data.applicant_name}</span>
						<i class="col-red fl mt15 ml10">*</i>
					</div>
				</div>

				<div class="row">
					<div class="form-group col-md-6 clearfix">
						<label>有无转介绍</label>
						<select class="fl" id="is_introducer" style="width: 40%;">
							<option value="0">请选择</option>
							<option value="1">有转介绍</option>
							<option value="2">无转介绍</option>
						</select>
					</div>
					<div class="form-group col-md-6 clearfix">
						<label>转介绍人</label>
						<!-- <input type="text" onclick="openModel(2)" value="{$data.introducer_telphone}" class="form-control"  id="customer_introducer_name" maxlength="11" readonly="readonly"/> -->
						<input type="text" value="{$data.introducer_telphone}" {if condition="isset($is_admin) AND $is_admin != 1"} readonly {/if} class="form-control" name="customer_introducer_name" id="customer_introducer_name" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="请输入转介绍人合同编号" maxlength="11"/>
						<!-- <input type="hidden" value="{$data.introducer_id}" name="introducer_id" id="introducer_id" > -->
						<span class="customer_introducer_name_span fl mt15 ml10">{$data.introducer_username}</span>
						<!--<i class="col-red fl mt15 ml10">*</i>-->
					</div>
				</div>

				<div class="row">
					<div class="form-group col-md-6 clearfix radiolist">
						<label>有无讲解</label>
						<div class="radio-inline">
							<input type="radio" name="is_explain" value="2" {if condition="($data.is_explain == 2)"} checked {/if}><span>无</span>
						</div>
						<div class="radio-inline">
							<input type="radio" name="is_explain" value="1" {if condition="($data.is_explain == 1)"} checked {/if}><span>有</span>
						</div>
					</div>
					<div class="form-group col-md-6 clearfix">
						<label>收据编号</label>
						<input type="text" value="{$data.receipt}" class="form-control" id="receipt" name="receipt" placeholder="请输入收据编号(7-10位纯数字)" maxlength="10" minlength="7" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
						<i class="col-red fl mt15 ml10">*</i>
					</div>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>合同编号</label>
					<input type="text" class="form-control" {if condition="($is_admin != 1)" }readonly="readonly" {/if} value="{$data.contract_no}" name="contract_no" placeholder="请输入合同编号" maxlength="10" minlength="7" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
					<input type="hidden" name="hidden_contract_no" value="{$data.contract_no}">
					<i class="col-red fl mt15 ml10">*</i>
				</div>

				<div class="form-group col-md-6 clearfix">
					<label>合同金额</label>
					<input type="text" class="form-control" value="{$data.contract_money}" name="contract_money" placeholder="请输入合同金额" onkeyup="checkMoney(this)">
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>联系人</label>
					<input type="text" class="form-control" value="{$data.contact_person}" name="contact_person" maxlength="5" placeholder="请输入联系人">
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>装机时间</label> {if condition="($is_admin == 1)"}
					<input type="text" id="installed_time" value="{$data.installed_time}" name="installed_time" readonly="readonly" class="form-control form_datetime"> {else /}
					<input type="text" id="installed_time" value="{$data.installed_time}" name="installed_time" readonly="readonly" class="form_datetime"> {/if}
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>联系电话</label>
					<input type="text" class="form-control" value="{$data.phone}" name="phone" maxlength="11" placeholder="请输入联系电话" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
					<i class="col-red fl mt15 ml10">*</i>
				</div>

				<div class="form-group col-md-6 clearfix">
					<label>续费周期</label>
					<input type="text" name="renew_date" id="renew_date" value="{$data.renew_date}" class="form-control" placeholder="请输入续费周期" onkeyup="this.value=this.value.replace(/\D/g,'')"><span class="fl mt10 fs12 mr5 col-666">年</span>
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<!--<div class="form-group col-md-6 clearfix">-->
				<!--<label>工程人员</label>-->
				<!--<input type="text" class="form-control" name="engineers_name" placeholder="请输入工程人员">-->
				<!--</div>-->

				<div class="form-group col-md-6 clearfix">
					<label>换芯周期</label>
					<input type="text" id="for_core_cycle" value="{$data.for_core_cycle}" name="for_core_cycle" class="form-control" placeholder="请选择换芯周期(月)" onkeyup="this.value=this.value.replace(/\D/g,'')"><span style="line-height: 34px" class="ml5 fl mr5">月</span>
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>水压</label>
					<input type="text" value="{$data.water_pressure}" id="water_pressure" name="water_pressure" class="form-control" placeholder="请输入水压"><span class="fl mt10 fs12 mr5 col-666">MPa</span>
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>客户性质</label>
					<select class="fl" name="nature" id="nature" onchange="removeNature()" style="width: 40%;">
						{volist name="$select.nature" id="nature"}
						<option value="{$i}" {if condition='($data.nature == $i)' }selected{/if}>{$nature}</option>
						{/volist}
					</select>
					<input class=" form-control" style="width:27%;" type="text" id="corporate_name" name="corporate_name" value="{$data.corporate_name}" placeholder="请输入公司名称">
					<i class="col-red fl mt15 ml10">*</i> {volist name="$select.is_nature" id="is_nature"}
					<input type="hidden" class="is_nature" value="{$is_nature}"> {/volist}
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>住房性质</label>
					<select class="fl" name="housing_property" style="width: 40%;">
						{volist name="$select.housing_property" id="housing_property"}
						<option value="{$i}" {if condition='($data.housing_property == $i)' }selected{/if}>{$housing_property}</option>
						{/volist}
					</select>
				</div>
				<div class="form-group col-md-6 clearfix">

					<label>最近一次换芯时间</label>
					<input type="text" name="recent_time" value="{$data.recent_time}" id="recent_time" class="form_datetime form-control" placeholder="请选择最近一次换芯时间" readonly="readonly" style="width: 40%;">
					<i class="col-red fl mt15 ml10">*</i>

				</div>
				<div class="form-group col-md-6 clearfix">
					<label>服务金额</label>
					<input type="number" name="for_core_cost" value="{$data.for_core_cost}" class="form-control" placeholder="请输入服务金额" style="width: 40%;"><span class="fl mt10 fs12 mr5 col-666">元</span>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>是否升级VIP</label>
					<select name="contract_type" class="fl">
						<option value="0" <?php if($data['contract_type']==0){echo 'selected';}?>>未升级VIP</option>
						<option value="1" <?php if($data['contract_type']==1){echo 'selected';}?>>已升级VIP</option>
					</select>
				</div>

				<div class="form-group col-md-6 clearfix" id="eq_count">
					<label>折算台数</label>
					<input type="number" name="eq_count" value="{$data.eq_count}" class="form-control" placeholder="请输入台数" style="width: 40%;"><span class="fl mt10 fs12 mr5 col-666">台</span>
					<i class="col-red fl mt15 ml10">请与财务沟通台数</i>
				</div>

				<div class="form-group col-md-6 clearfix">
					<label>特殊合同</label>
					<select class="fl" name="is_special" style="width: 18%;">
						<option value="1" {if condition="($data.is_special == 1)"} selected {/if}>是</option>
						<option value="2" {if condition="($data.is_special == 2)"} selected {/if}>否</option>
					</select>
					<i class="col-red fl mt15 ml10">注：特殊合同将不能提交到业绩系统</i>
				</div>

				<div class="Installed_model">
					{if condition="($data.equipments)"} {volist name="$data.equipments" id="equipments" offset="0" length='1'}
					<div class="form-group col-md-12 clearfix equipments_number">
						<input type="hidden" name="id[]" value="{$equipments.id}">
						<label>装机型号</label>
						<select class="fl" name="equipments_name[]" style="width:19.5%">
							{volist name="$select.equipments_name" id="equipments_name"}
							<option value="{$equipments_name.equipments_id}" {if condition='($equipments.equipments_id == $equipments_name.equipments_id)' }selected{/if}>{$equipments_name.equipments_name}</option>
							{/volist}
						</select>
						<input type="text" readonly="readonly" value="{$equipments.equipments_number}" name="equipments_number[]" id="EquipmentsNumber" placeholder="请输入设备号" />
						<div class="btn btn-default fl" {if condition='(empty($equipments.equipments_number))' } onclick="getEquipmentsNumber(this)" {/if}>自动生成&nbsp;&nbsp;<i class="glyphicon glyphicon-repeat"></i></div>
						<p class="fl clearfix"><span class="fl mt10 fs12 ml5 mr5 col-666">免费移机次数</span><input style="width:60px !important" type="text" name="moving_machine_number[]" value="{$equipments.moving_machine_number}" onkeyup="this.value=this.value.replace(/\D/g,'')" /><span class="fl mt10 fs12 mr5 col-666">次</span></p>
						<i class="col-red fl mt15 ml5 mr5">*</i>
						<div class="btn btn-default fl" onclick="addmodel()"><i class="glyphicon glyphicon-plus"></i></div>
					</div>
					{/volist} {volist name="$data.equipments" id="equipments" offset="1"}
					<div class="form-group col-md-12 clearfix">
						<input type="hidden" name="id[]" value="{$equipments.id}">
						<label></label>
						<select class="fl" name="equipments_name[]" style="width:19.5%">
							<option value="">请选择设备型号</option>
							{volist name="$select.equipments_name" id="equipments_name"}
							<option value="{$equipments_name.equipments_id}" {if condition='($equipments.equipments_id == $equipments_name.equipments_id)' }selected{/if}>{$equipments_name.equipments_name}</option>
							{/volist}
						</select>
						<input type="text" readonly="readonly" value="{$equipments.equipments_number}" name="equipments_number[]" id="EquipmentsNumber" placeholder="请输入设备号" />
						<div class="btn btn-default fl" {if condition='(empty($equipments.equipments_number))' } onclick="getEquipmentsNumber(this)" {/if}>自动生成&nbsp;&nbsp;<i class="glyphicon glyphicon-repeat"></i></div>
						<p class="fl clearfix"><span class="fl mt10 fs12 ml5 mr5 col-666">免费移机次数</span><input style="width:60px !important" type="text" name="moving_machine_number[]" value="{$equipments.moving_machine_number}" onkeyup="this.value=this.value.replace(/\D/g,'')" /><span class="fl mt10 fs12 mr5 col-666">次</span></p>
						<i class="col-red fl mt15 ml5 mr5">*</i>
						<div class="btn btn-default fl" onclick="delmodel(this)" style="margin-right: 5px;"><i class="glyphicon glyphicon-minus"></i></div>
					</div>
					{/volist} {else /}
					<div class="form-group col-md-12 clearfix">
						<input type="hidden" name="id[]" value="">
						<label>装机型号</label>
						<select class="fl" name="equipments_name[]" style="width:19.5%">
							<option value="">请选择设备型号</option>
							{volist name="$select.equipments_name" id="equipments_name"}
							<option value="{$equipments_name.equipments_id}">{$equipments_name.equipments_name}</option>
							{/volist}
						</select>
						<input type="text" value="" readonly="readonly" name="equipments_number[]" id="EquipmentsNumber" placeholder="请输入设备号" />
						<div class="btn btn-default fl" onclick="getEquipmentsNumber(this)">自动生成&nbsp;&nbsp;<i class="glyphicon glyphicon-repeat"></i></div>
						<p class="fl clearfix"><span class="fl mt10 fs12 ml5 mr5 col-666">免费移机次数</span><input style="width:60px !important" type="text" name="moving_machine_number[]" value="1" onkeyup="this.value=this.value.replace(/\D/g,'')" /><span class="fl mt10 fs12 mr5 col-666">次</span></p>
						<i class="col-red fl mt15 ml5 mr5">*</i>
						<div class="btn btn-default fl" onclick="addmodel()"><i class="glyphicon glyphicon-plus"></i></div>
					</div>
					{/if}
				</div>

				<div class="form-group col-md-6 clearfix">
					<label>换芯时间</label>
					<input type="text" id="for_core_time" name="for_core_time" value="{$data.for_core_time}" readonly class="form_datetime form-control" placeholder="">
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>总免费换芯次数</label>
					<input type="text" id="for_core_number" name="for_core_number" value="{$data.for_core_number}" class="form-control" onkeyup="this.value=this.value.replace(/\D/g,'')">次
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>到期时间</label>
					<input type="text" id="exire_date" value="{$data.exire_date}" name="exire_date" readonly class="form_datetime form-control" placeholder="请选择到期时间">
					<input type="hidden" id="hidden_exire_date" value="{$data.exire_date}">
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>总免费移机次数</label>
					<input type="text" id="contract_moving_machine_number" name="contract_moving_machine_number" class="form-control" value="{$data.moving_machine_number}" onkeyup="this.value=this.value.replace(/\D/g,'')">次
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-6 clearfix">
					<label>合同状态</label>
					<select class="fl" name="contract_status" style="width: 40%;">
						{volist name="$select.contract_status" id="contract_status"}
						<option value="{$i}" {if condition='($data.contract_status == $i)' }selected{/if}>{$contract_status}</option>
						{/volist}
					</select>
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-12 clearfix">
					<label>安装地址</label>
					<select class="fl" id="province" name="province_code" style="width: 13%;" onchange="moveCity()">
						<option value="">请选择省</option>
						{volist name="$Province" id="Province"}
						<option value="{$Province.id}" {if condition='($data.province_code == $Province.id)' }selected{/if}>{$Province.area_name}</option>
						{/volist}
					</select>
					<select class="fl" id="city" name="city_code" style="width: 13%;" onchange="moveArea()">
						<option value="">请选择市</option>
						{volist name="$City" id="City"}
						<option value="{$City.id}" {if condition='($data.city_code == $City.id)' }selected{/if}>{$City.area_name}</option>
						{/volist}
					</select>
					<select class="fl" id="Area" name="area_code" style="width: 13%;">
						<option value="">请选择区/县</option>
						{volist name="$Area" id="Area"}
						<option value="{$Area.id}" {if condition='($data.area_code == $Area.id)' }selected{/if}>{$Area.area_name}</option>
						{/volist}
					</select>
					<input type="text" name="address" class="form-control fl" placeholder="安装地址" value="{$data.address}" style="width: 30%;">
					<i class="col-red fl mt15 ml10">*</i>
				</div>
				<div class="form-group col-md-12 relative clearfix" id="wordCount">
					<label>备注</label>
					<textarea name="remark" class="form-control" style="width: 70%;" rows="8">{$data.remark}</textarea>
					<span class="wordwrap1 absolute col-666" style="right: 19%; bottom: 10px;"><var class="word col-666" style="font-style: normal;">500</var>/500</span>
				</div>

				<div class="col-md-12 upload_contract clearfix">
					<label class="fl col-md-2">上传合同</label>
					<div class="enclosurelist fl col-md-10">
						{volist name="$data.contract_imgs" id="img"}
						<div class="enclosureitem fl">
							<img id="img" src="{:Config('CONTRACT_IMAGES_PATH')}{$img}" />
							<input type="hidden" name="contract_imgs[]" value="{$img}">
							<i class="glyphicon glyphicon-remove" onclick="delcontract(this)"></i>
						</div>
						{/volist}
						<!--<div class="enclosureitem fl">-->
						<!--<img src="__IMG__/enclosure.png" />-->
						<!--<i class="glyphicon glyphicon-remove" onclick="delcontract(this)"></i>-->
						<!--</div>-->
						<!--<div class="enclosureitem fl">-->
						<!--<img src="__IMG__/enclosure.png" />-->
						<!--<i class="glyphicon glyphicon-remove" onclick="delcontract(this)"></i>-->
						<!--</div>-->
						<div class="upload fl">
							<i class="glyphicon glyphicon-plus"></i>
							<input type="file" name="avatar_img" id="avatar_img" multiple="multiple" onchange="uploadFile()" accept="image/jpg,image/jpeg,image/png" />
						</div>
						<div class="clearfix"></div>
						<p>备注：支持上传JPG、JPEG、PONG、GIF格式图片上传，最多上传6张</p>
					</div>
				</div>
			</div>
		</div>
		<input type="hidden" value="{$data.contract_id}" name="contract_id">
		<p class="engineer-Btn">
			<button id="PreBtn" class="engineer-PreBtn" type="button">保存</button>
			<input type="hidden" value="{$data.user_id}" name="user_id" id="user_id">
			<!--<a href="javascript:(0)" class="engineer-ResetBtn" onclick="reset()">重置</a>-->
			<a href="javascript:(0)" class="engineer-RuterBtn" onclick="window.history.go(-1);">返回</a>
		</p>
	</form>
</div>

<script>
    window.onbeforeunload = function()
    {
        //  这是用来设定一个时间, 时间到了, 就会执行一个指定的 method。
        setTimeout(onunloadcancel, 10);
        return "真的离开?";
    }
    window.onunloadcancel = function()
    {
        // alert("取消离开");
    }

	//计算换芯次数和移机次数
	function countNumber() {
		var num = 0;
		$(".equipments_number").each(function(i) {
			num = i + 1;
		})
		$("#for_core_number").val(num);
		$("#contract_moving_machine_number").val(num);
	}
	//计算到期时间
	function exire_date() {
		var installed_time = $("#hidden_exire_date").val();
		//获取年月日
		var installed_time_year = parseInt($("#installed_time").val().substr(0, 4));
		var installed_time_month = parseInt($("#installed_time").val().substr(5, 2));
		var installed_time_day = parseInt($("#installed_time").val().substr(8, 2));
		//特殊处理
		if(installed_time_day - 1 == 0) {
			installed_time_month = installed_time_month - 1;
			var numberDay = getDaysInMonth(installed_time_year, installed_time_month);
			installed_time_day = numberDay + 1;
		} else {
			installed_time_day = installed_time_day;
		}

		//月数和日数小于9时，前面加0
		if(installed_time_month < 10) {
			installed_time_month = "0" + installed_time_month;
		}
		if(installed_time_day < 10) {
			installed_time_day = "0" + installed_time_day;
		}
		if(installed_time == '') {
			var mydate = new Date();
			var year = parseInt(mydate.getFullYear()) + parseInt($("#renew_date").val());
			var nowTime = "" + year + "-";
			nowTime += (mydate.getMonth() + 1) > 9 ? (mydate.getMonth() + 1) : '0' + (mydate.getMonth() + 1);
			nowTime += "-";
			nowTime += (mydate.getDate()) > 9 ? (mydate.getDate()) : '0' + (mydate.getDate());
			$("#exire_date").val(nowTime);
		} else {
			installed_time_year = installed_time_year + parseInt($("#renew_date").val());
			//拼接日期格式
			var time = installed_time_year + "-" + installed_time_month + "-" + installed_time_day;
			//赋值
			$("#exire_date").val(time);
		}
	}
	$("#renew_date").change(function() {
		exire_date();
	});
	//计算换芯时间
	$("#for_core_cycle").change(function() {
		for_core_time();
	});
	$("#recent_time").change(function() {
		for_core_time();
	});
	$("#installed_time").change(function() {
		//最后一次换芯时间
		if($("#recent_time").val() == '') {
			$("#recent_time").val($("#installed_time").val());
		}
		exire_date();
		for_core_time();
	})

	function for_core_time() {
		//获取年月日
		var installed_time_year = parseInt($("#recent_time").val().substr(0, 4));
		var installed_time_month = parseInt($("#recent_time").val().substr(5, 2));
		var installed_time_day = parseInt($("#recent_time").val().substr(8, 2));
		//获取换芯周期
		var for_core_cycle = parseInt($("#for_core_cycle").val());
		//获取总共月数
		var month = installed_time_month + for_core_cycle;
		//判断月数，算出相应的年数
		if(month > 12) {
			if(month % 12 == 0) {
				installed_time_year = installed_time_year + month / 12;
			} else {
				installed_time_year = installed_time_year + parseInt(month / 12);
				installed_time_month = month % 12;
			}
		} else {
			installed_time_month = installed_time_month + for_core_cycle;
		}
		//特殊处理
		if(installed_time_day - 1 == 0) {
			installed_time_month = installed_time_month - 1;
            if (installed_time_month == 0){
                installed_time_year = installed_time_year - 1;
                installed_time_month = 12;
            }
			var numberDay = getDaysInMonth(installed_time_year, installed_time_month);
			installed_time_day = numberDay;
		} else {
			installed_time_day = installed_time_day - 1;
		}

		//月数和日数小于9时，前面加0
		if(installed_time_month < 10) {
			installed_time_month = "0" + installed_time_month;
		}
		if(installed_time_day < 10) {
			installed_time_day = "0" + installed_time_day;
		}
		//拼接日期格式
		var time = installed_time_year + "-" + installed_time_month + "-" + installed_time_day;
		//赋值
		$("#for_core_time").val(time);
	}

	function checkMoney(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
	}
	//隐藏公司名称input
	removeNature();

	function removeNature() {
		var nature = $("#nature option:selected").text()
		$(".is_nature").each(function() {
			if($(this).val() != nature) {
				$("#corporate_name").css('display', 'none');
                $("#eq_count").css('display', 'none');//折算台数
			} else {
				$("#corporate_name").css('display', '');
                $("#eq_count").css('display', '');//折算台数
			}
		})
	}
	//上传图片
	function uploadFile() {
		var data = new FormData($("#form")[0]);
		$.each($('#avatar_img')[0].files, function(i, file) {
			data.append('upload_file', file);
		});
		$('#avatar_img').val('');
		$.ajax({
			url: "{:url('Contractinfo/uploadFile')}",
			type: 'POST',
			data: data,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			dataType: 'json',
			success: function(returndata) {
				if(returndata.code == '1') {
					console.log(returndata)
					var html = '';
					html += '<div class="enclosureitem fl">';
					html += '<img id="img" src="{:Config('CONTRACT_IMAGES_PATH')}' + returndata.data + '" />';
					html += '<i class="glyphicon glyphicon-remove" onclick="delcontract(this)"></i>';
					html += '</div>';
					html += '<input type="hidden" name="contract_imgs[]" value="' + returndata.data + '">';
					$(".upload").before(html);
				} else {
					parent.layer.msg(returndata.msg, {
						icon: 2,
						time: 2000
					}, function() {});
				}

			},
			error: function(returndata) {
				parent.layer.msg('数据异常', {
					icon: 2,
					time: 2000
				});
				return false;
			}
		});
	}

	//提交数据
	$("#PreBtn").click(function() {
	    if ($("#is_introducer").val() == 0){
            parent.layer.msg('请选择转介绍', {
                icon: 2,
                time: 2000
            }, function() {});
            return false;
		}
		if ($("#is_introducer").val() == 1 && $("#customer_introducer_name").val() == ''){
            parent.layer.msg('请输入转介绍的合同编号', {
                icon: 2,
                time: 2000
            }, function() {});
            return false;
		}
		var formData = new FormData($("#form")[0]);
		$.ajax({
			url: "{:url('Contractinfo/doEditContract')}",
			type: 'POST',
			data: formData,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			dataType: 'json',
			success: function(returndata) {
				if(returndata.code == '1') {
					parent.layer.msg(returndata.msg, {
						icon: 1,
						time: 2000
					}, function() {
						history.back();
					});
				} else {
					parent.layer.msg(returndata.msg, {
						icon: 2,
						time: 2000
					}, function() {});
				}

			},
			error: function(returndata) {
				parent.layer.msg('数据异常', {
					icon: 2,
					time: 2000
				});
				return false;
			}
		});

	})

	//获取设备编号
	function getEquipmentsNumber(obj) {
		$.get("{:url('Contractinfo/getEquipmentsNumber')}", {}, function(flag) {
			console.log(flag);

			$(obj).prev().val(flag);

		});
	}

	//选择地址
	function moveCity() {
		$("#Area").html('');
		var province = $("#province").val();
		var str1 = '<option value="">请选择区/县</option>';
        $("#Area").html(str1);
		$.get("{:url('/admin/Three/city')}", {
			parent_id: province
		}, function(data) {
			console.log(data);
			$("#city").empty();
			$("#city").append($("<option>").val("").text("请选择市"));
			for(var i = 0; i < data.length; i++) {
				var option = $("<option>").val(data[i].id).text(data[i].area_name);
				$("#city").append(option);
			}

		})
	}

	//选择地址
	function moveArea() {
		var city = $("#city").val();
		$.get("{:url('/admin/Three/areas')}", {
			city_id: city
		}, function(data) {
			$("#Area").empty();
			$("#Area").append($("<option>").val("").text("请选择区/县"));
			for(var i = 0; i < data.length; i++) {
				var option = $("<option>").val(data[i].id).text(data[i].area_name);
				$("#Area").append(option);
			}

		})
	}

	function openModel(type) {
		var html = '';
		html += '<div id="userh">';
		html += '<div class="modal-body">';
		html += '<h2 class="fs14 col-666 mb10">选择用户</h2>';
		html += '<p>';
		html += '<input class="modalUser-input" type="text" id="susername" name="susername" placeholder="请输入用户">';
		html += '<input class="modalUser-input" type="text" id="stelphone" name="stelphone" placeholder="请输入联系电话">';
		html += '<input class="modalUser-input" type="text" id="sid_card" name="sid_card" placeholder="请输入身份证号码">';
		html += '<button class="btn btn-primary mr10 pl15 pr15 pt3 pb3" onclick="searchCustomer()">搜索</button>';
		html += '<button class="btn btn-primary pl15 pr15 pt3 pb3" onclick="jumpcustome()"><i class="glyphicon glyphicon-plus"></i>&nbsp;&nbsp;新增用户</button>';
		html += '</p>';
		html += '<table class="table table-bordered mt20">';
		html += '<thead>';
		html += '<tr>';
		html += '<th></th>';
		html += '<th>账号</th>';
		html += '<th>户名</th>';
		html += '<th>联系人电话</th>';
		html += '<th>身份证号</th>';
		html += '<th>账号状态</th>';
		html += '</tr>';
		html += '</thead>';
		html += '<tbody>';

		html += '<script id="list-template" type="text/html">';
		html += '{{# for(var i=0;i';
		html += '<d.length;i++){ }} <tr>';
		html += '<td><input type="radio" value="{{d[i].user_id}}" name="user_id"><input type="hidden" value="{{d[i].username}}" id="cusername{{d[i].user_id}}"><input type="hidden" value="{{d[i].telphone}}" id="telphone{{d[i].user_id}}"></td>';
		html += '<td>{{d[i].telphone}}</td>';
		html += '<td >{{d[i].username}}</td>';
		html += '<td>{{d[i].telphone}}</td>';
		html += '<td>{{d[i].id_card}}</td>';
		html += '<td>{{d[i].account_status}}</td>';
		html += '</tr>';
		html += '{{# } }}';
		html += '<\/script>';
		html += '</tbody>';
		html += '<tbody id="list-content"></tbody>';
		html += '</table>';

		html += '<div class="">';
		html += '<div class=" clearfix">';
		html += '<div class="clearfix" style="float:right;margin: 0 auto;">';
		html += '<span class="fl mt5" id="count">共0条数据</span><span id="allpage"  class="fl  mr20 mt5"></span><span  class="fl" id="AjaxPage"></span>';
		html += '</div>';
		html += '<div class="spiner-example">';
		html += '<div class="sk-spinner sk-spinner-three-bounce">';
		html += '<div class="sk-bounce1"></div>';
		html += '<div class="sk-bounce2"></div>';
		html += '<div class="sk-bounce3"></div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';

		html += '<div class="modal-footer t_center">';
		html += '<button type="button" onclick="subback(' + type + ')" class="btn btn-primary ml30 mr30 pl20 pr20 pt3 pb3">确认</button>';
		html += '<button type="button" class="btn btn-default ml30 mr30 pl20 pr20 pt3 pb3" onclick="cancelmodel()">取消</button>';
		html += '</div>';
		html += '</div>';
		//页面层
		parent.layer.open({
			title: '选择客户',
			type: 1,
			skin: 'layui-layer-rim', //加上边框
			area: ['823px', '490px'], //宽高
			content: html
		});
		Ajaxpage(1);
	}

	//    //重置
	//    function reset() {
	//        top.layer.confirm('你确定要重置吗?', {
	//            btn: ['确定', '取消'] //按钮
	//        }, function() {
	//            top.layer.msg('重置成功', {
	//                icon: 1
	//            });
	//
	//        });
	//    }

//	//重置
//	function reset() {
//		top.layer.confirm('你确定要重置吗?', {
//			btn: ['确定', '取消'] //按钮
//		}, function() {
//			$("#form")[0].reset();
//			top.layer.msg('重置成功', {
//				icon: 1
//			});
//
//		});
//	}

	//增加 删除
	var i = 0;

	function addmodel() {
		i++;
		var html = '<div class="form-group col-md-12 clearfix equipments_number">';
		html += '<input type="hidden" name="id[]" value="">';
		html += '<label></label>';
		html += '<select class="fl" name="equipments_name[]" style="margin-right: 4px; margin-left: 4px;width:19.5%">';
		html += '<option value="">请选择设备型号</option>';
		html += '{volist name="$select.equipments_name" id="equipments_name"}';
		html += '<option value="{$equipments_name.equipments_id}">{$equipments_name.equipments_name}</option>';
		html += '{/volist}';
		html += '</select>';
		html += '<input type="text"  name="equipments_number[]" id="EquipmentsNumber' + i + '" placeholder="请输入设备号" style="margin-right: 4px;" />';
		html += '<div class="btn btn-default fl" onclick="getEquipmentsNumber(this)">自动生成&nbsp;&nbsp;<i class="glyphicon glyphicon-repeat"></i></div>';
		html += '<p class="fl clearfix"><span class="fl mt10 fs12 ml5 mr5 col-666">免费移机次数</span><input style="width:60px !important" type="text" name="moving_machine_number[]" value="1" /><span class="fl mt10 fs12 mr5 col-666">次</span></p>';
		html += '<i class="col-red fl mt15 ml5 mr5">*</i>';
		html += '<div class="btn btn-default fl" onclick="delmodel(this)" style="margin-right: 5px;"><i class="glyphicon glyphicon-minus"></i></div>';
		html += '</div>';
		$('.Installed_model').append(html);
		countNumber();
	}

	function delmodel(obj) {
		i--;
		$(obj).parent().remove();
		countNumber();
	};

	function addparts() {
		var html = '<div class="form-group col-md-12 clearfix">';
		html += ' <input type="hidden" name="contract_parts_id[]" value="">';
		html += ' <label></label>';
		html += '<select name="parts_id[]" id="" class="fl">';
		html += '{volist name="$select.Parts" id="Parts"}';
		html += '<option value="{$Parts.parts_id}">{$Parts.parts_name}</option>';
		html += '{/volist}';
		html += '</select>';
		html += '<input type="text" name="parts_number[]" value="1" class="form-control" style="width: 10%">';
		html += ' <span style="line-height: 34px" class="ml5 fl mr5">数量</span>';
		html += '<div class="btn btn-default fl" onclick="delparts(this)"><i class="glyphicon glyphicon-minus"></i></div>';
		html += '</div>'
		$('.addparts').append(html);
	}

	function delparts(obj) {
		$(obj).parent().remove();
	}

	//删除合同
	function delcontract(obj) {
		parent.layer.confirm('确定删除该图片？', {
			btn: ['确定', '取消'] //按钮
		}, function() {
			parent.layer.msg('删除成功', {
				icon: 1,
				time: 1000
			}, function() {
				$(obj).parent().remove();
			});
		});
	}

	//剩余字数统计
	$(function() {
		//先选出 textarea 和 统计字数 dom 节点
		var wordCount = $("#wordCount"),
			textArea = wordCount.find("textarea"),
			word = wordCount.find(".word");
		//调用
		statInputNum(textArea, word);
	});
	//注意 最大字数只需要在放数字的节点哪里直接写好即可 如：<var class="word">200</var>
	function statInputNum(textArea, numItem) {
		var max = numItem.text(),
			curLength;
		textArea[0].setAttribute("maxlength", max);
		curLength = textArea.val().length;
		numItem.text(max - curLength);
		textArea.on('input propertychange', function() {
			var _value = $(this).val().replace(/\n/gi, "");
			numItem.text(max - _value.length);
		});
	}

	//日期选择
	$(".form_datetime").datetimepicker({
		format: "yyyy-mm-dd",
		autoclose: true,
		minView: "month",
		todayBtn: true,
		pickerPosition: "bottom-left",
		language: 'cn'
	});

	//选择用户
	function myuserContract() {
		$(".spiner-example").css('display', 'block'); //数据加载完关闭动画
		Ajaxpage();
		setTimeout(function() {
			var html = $("#userh").html();
			top.layer.open({
				type: 1,
				title: '选择用户',
				skin: 'layui-layer-demo', //样式类名
				closeBtn: 0, //不显示关闭按钮
				anim: 2,
				area: ['700px', '350px'], //宽高
				shadeClose: true, //开启遮罩关闭
				content: html
			});
			$(".spiner-example").css('display', 'none'); //数据加载完关闭动画
		}, 1000)

	}

	function Ajaxpage(curr) {
		$.getJSON('{:url("Contractinfo/getCustomer")}', {
			page: curr || 1
		}, function(data) {
			console.info(data);
			console.log(curr)
			parent.$(".spiner-example").css('display', 'none'); //数据加载完关闭动画
			if(data.data == '') {
				parent.$("#list-content").html('<td colspan="20" style="padding-top:10px;padding-bottom:10px;font-size:16px;text-align:center">暂无数据</td>');
				parent.$('#count').html("共" + data.page.count + "条数据");
			} else {
				var tpl = parent.document.getElementById('list-template').innerHTML;
				parent.laytpl(tpl).render(data.data, function(html) {
					parent.$('#list-content').html(html);
					parent.$('#count').html("共" + data.page.count + "条数据");
				});
				parent.laypage({
					cont: parent.$('#AjaxPage'), //容器。值支持id名、原生dom对象，jquery对象,
					pages: data.page.lastPage, //总页数
					skip: true, //是否开启跳页
					skin: '#337ab7', //分页组件颜色
					curr: curr || 1,
					first: '首页', //若不显示，设置false即可
					last: '尾页', //若不显示，设置false即可
					groups: 3, //连续显示分页数
					jump: function(obj, first) {
						if(!first) { 
							Ajaxpage(obj.curr)
						}
						parent.$('#allpage').html('，第' + obj.curr + '页，共' + '<i>' + obj.pages + '</i>' + '页');
						inputlength();
					}
				});
			}
		});
	}
</script>

</body>

</html>